﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CentralOffice.PublicAffair.Data;

using PostTable = CentralOffice.PublicAffair.Data.PostDataSet.PostsDataTable;
using PostRow = CentralOffice.PublicAffair.Data.PostDataSet.PostsRow;
using Husb.Util;


namespace CentralOffice.WebClient.PublicAffairs
{
    
    public partial class PostQuery : PageBase
    {
        private bool? canQueryAll = null;
        private bool? canManage = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                SiteUtil.PopulateListBox(ddlPostType, SiteConstants.PostType, true);
            }

            if (canManage == null)
            {
                canManage = this.Authorize(AuthorizationRules.Post_Manage);
            }
            if (canQueryAll == null)
            {
                canQueryAll = this.Authorize(AuthorizationRules.Post_SelectAll);
            }
            //if (!authorized.Value)
            //{
            //    gv.Columns.RemoveAt(gv.Columns.Count - 1);
            //    gv.Columns.RemoveAt(gv.Columns.Count - 1);
            //}
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            gv.PageIndex = 0;

            PostRow post = (new PostDataSet()).Posts.NewPostsRow();

            post.Name = txtName.Text;
            if (ddlPostType.SelectedIndex > 0 )
            {
                post.Category = Int32.Parse( ddlPostType.SelectedItem.Value);
            }

            post.ContentText = txtContent.Text;
            post.Author = txtAuthor.Text;

            if (txtPostDate.Text.Length > 0)
            {
                post.CreatedTime = DateTime.Parse(txtPostDate.Text);
            }
            if (txtPostDateEnd.Text.Length > 0)
            {
                post.CreatedTimeEnd = DateTime.Parse(txtPostDateEnd.Text);
            }

            odsPost.SelectParameters.Clear();
            odsPost.SelectParameters.Add("whereClause", post.FilterExpression);
            //odsTask.SelectParameters["whereClause"].DefaultValue = post.FilterExpression;//companyInfo.FilterExpression
        }

        protected void gv_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                System.Data.DataRowView row = e.Row.DataItem as System.Data.DataRowView;
                PostRow post = row.Row as PostRow;
                string author = post.Author;

                // 如果没有管理所有帖子的权限
                if (!canManage.Value)
                {
                    if (author.IndexOf(CommonUtil.TrimAll(ApplicationContext.ActualUserName)) == -1 && gv.Columns.Count > 5)
                    {
                        e.Row.Cells[5].Controls.Clear();
                        e.Row.Cells[4].Controls.Clear();

                        //gv.Columns.RemoveAt(gv.Columns.Count - 1);
                        //gv.Columns.RemoveAt(gv.Columns.Count - 1);
                    }
                }

                
            }
        }

        protected void odsPost_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            //odsPost_Selecting
            //object s = e.InputParameters["whereClause"];

            // 如果没有权限查看所有帖子
            if (!canQueryAll.Value)
            {
                string where = String.Format(" AND (Author = '{0}' OR Accepter = '*' OR Accepter  LIKE '%{0}%') ", CommonUtil.TrimAll(ApplicationContext.ActualUserName));
                e.InputParameters["whereClause"] = e.InputParameters["whereClause"] + where;
            }
        }
    }
}
